package controllers
{
	import models.Diagram;
	import models.Project;
	import models.RESTResponse;
	import models.User;
	
	import mx.collections.ArrayCollection;
	
	public class ProjectManager
	{
		[Bindable]
		private var _projects:ArrayCollection;
		[Bindable]
		private var _current:Project;
		
		public function ProjectManager() {
			_projects 	= new ArrayCollection();
			_current 	= null;
		}
		
		public function wakeUp() : void {
			updateList();
		}

		public function updateList() : void {
			HTTPController.GET('/projects', null, updateListHandler);
		}
		
		public function updateListHandler(response:RESTResponse) : void {
			if(response.success()) {
				/* ATTENTION ! ne pas faire de new ... ça casse le data binding ! */
				_projects.removeAll();
				for each(var xml:XML in response.data()) {
					var p:Project = new Project();
					p.setXML(xml);
					_projects.addItem(p);
				}
			} else {
				UIErrorController.handleRESTErrorCode(response.errorCode());
			}
		}
		
		public function createProject(project:Project) : void {
			HTTPController.POST('/projects', project.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					var xml:XML = response.dataNode();
					var p:Project = new Project();
					p.setXML(xml);
					_projects.addItem(p);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function updateProject(project:Project) : void {
			HTTPController.PUT('/projects', project.toXML(), function (response:RESTResponse) : void {
				if(response.success()) {
					var xml:XML = response.dataNode();
					var p:Project = new Project();
					p.setXML(xml);
					_projects.removeItemAt(_projects.getItemIndex(project));
					_projects.addItem(p);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});	
		}
		
		public function handleProjectDiagrams(project:Project, response:RESTResponse) : void {
			if(response.success()) {
				if(_current != null && project.oid == _current.oid) {
					var index:int = _projects.getItemIndex(project)
					var diagrams:ArrayCollection = new ArrayCollection();
					var d:Diagram;
					for each(var xml:XML in response.data()) {
						d = new Diagram();
						d.setXML(xml);
						diagrams.addItem(d);
					}
					project.diagrams = diagrams;
					_projects.removeItemAt(index);
					_projects.addItemAt(project, index);
				}
			} else {
				UIErrorController.handleRESTErrorCode(response.errorCode());
			}
		}
		
		public function getProjectById(oid:int) : Project {
			for each(var project:Project in _projects) {
				if(project.oid == oid) {
					return project;
				}
			}
			return null;
		}
		
		public function saveProject(name:String, ownerId:int) : void {
			if(_current == null) {
				createProject(new Project(name, ownerId));
			} else {
				_current.name = name;
				_current.ownerId = ownerId;
				updateProject(_current);
				_current = null;
			}
		}
		
		public function get projects() : ArrayCollection {
			return _projects;
		}
		
		public function set currentProject(project:Project) : void {
			_current = project;
		} 
		
		public function get currentProject() : Project {
			return _current;
		}
		
		public function addContributor(project:Project, user:User) : void {
			HTTPController.POST('/projects/project/contributors' , new XML('<contribution projectId="' + project.oid + '" userId="' + user.oid + '"/>'), function (response:RESTResponse) : void {
				if(response.success()) {
					WindowManager.open(WindowManager.PROJECT_NO_DETAILS);
					updateList();
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function removeContributor(project:Project, user:User) : void {
			HTTPController.DELETE('/projects/project/' + project.oid + '/contributors/' + user.oid , null, function (response:RESTResponse) : void {
				if(response.success()) {
					WindowManager.open(WindowManager.PROJECT_NO_DETAILS);
					updateList();
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
	}
}